import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/login/index.vue";
import Layout from "@/views/layout/index.vue";
import Dashboard from "@/views/dashboard/index.vue";
import store from "@/store";
import Organization from "@/views/organization-manage";
import InstitutionsJobsArea from "@/views/institutions-jobs-area";
import Driver from "@/views/driver";
import FreightManage from "@/views/freight-manage/index.vue";
import CarModels from "@/views/car-models/index.vue";
import Vehicle from "@/views/vehicle/index.vue";
import CarRegister from "@/views/car-register/index.vue";
import OperationalRange from "@/views/operational-range/index.vue";
import WorkArrangeManage from "@/views/workArrange-manage/index.vue";
import OrderManage from "@/views/order-manage/index.vue";
import Waybill from "@/views/waybill/index.vue";
import DispatchTask from "@/views/dispatchTask/index.vue";
import PickUptask from "@/views/pickUptask/index.vue";
import LineManage from "@/views/line-manage/index.vue";
import TransportTask from "@/views/transport-task/index.vue";
import CarRegisterInfo from "@/views/car-register/car-register-info.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: Dashboard,
      },
      {
        path: "branches/organization-manage",
        component: Organization,
      },
      {
        path: "branches/institutions-jobs-area",
        component: InstitutionsJobsArea,
      },
      {
        path: "transit/driver",
        component: Driver,
      },
      {
        path: "transit/freight-manage",
        component: FreightManage,
      },
      {
        path: "transit/car-models",
        component: CarModels,
      },
      {
        path: "transit/vehicle",
        component: Vehicle,
      },
      {
        path: "transit/car-register",
        component: CarRegister,
      },
      {
        path: "branches/operational-range",
        component: OperationalRange,
      },
      {
        path: "transit/workArrange-manage",
        component: WorkArrangeManage,
      },
      {
        path: "business/order-manage",
        component: OrderManage,
      },
      {
        path: "business/waybill",
        component: Waybill,
      },
      {
        path: "transport/transport-task",
        component: TransportTask,
      },
      {
        path: "transport/line-manage",
        component: LineManage,
      },
      {
        path: "branches/pickUptask",
        component: PickUptask,
      },
      {
        path: "branches/dispatchTask",
        component: DispatchTask,
      },
      {
        path: "transit/car-register-info/:id",
        component: CarRegisterInfo,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  if (!token && to.path !== "/login") {
    next("/login");
  }
  next();
});
export default router;
